//
//  ChangePayPWController.m
//  PosApp
//
//  Created by 李英新 on 2018/10/3.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "ChangePayPWController.h"
static NSUInteger timerNum = 60;
static NSString * CodeStr;

@interface ChangePayPWController ()
@property (weak, nonatomic) IBOutlet UITextField *UseriPhone;
@property (weak, nonatomic) IBOutlet UITextField *CodeText;
@property (weak, nonatomic) IBOutlet UILabel *TimeLabel;
@property (weak, nonatomic) IBOutlet UIButton *TimeBtn;


@property (weak, nonatomic) IBOutlet UITextField *PayPW;
@property (weak, nonatomic) IBOutlet UITextField *PayPW01;
@property (strong, nonatomic) NSTimer * timer;

@end

@implementation ChangePayPWController

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.timer invalidate];
    timerNum = 60;
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"修改密码";
    
    self.UseriPhone.keyboardType = UIKeyboardTypeNumberPad;
    self.UseriPhone.keyboardType = UIKeyboardTypeNumberPad;
    
    self.TimeLabel.layer.cornerRadius = self.TimeLabel.bounds.size.height/2;
    self.TimeLabel.layer.borderWidth = 1;
    self.TimeLabel.layer.borderColor = KRGBColor(63, 207, 255).CGColor;
}
- (IBAction)ClickOnBtnTuoQuCode:(id)sender {
    
    if (![MainObjectWay IsMobileWithString:self.UseriPhone.text]) {
        [MBProgressHUD showAlertViewWithText:@"请输入正确的手机号"];
        return;
    }
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:self.UseriPhone.text forKey:@"phone"];
    
    [MainObjectWay PostWithMethod:@"send" andAction:@"Server" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"请求的信息 = %@",responseObject);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            self.TimeBtn.hidden = YES;
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(ChangeTime) userInfo:nil repeats:YES];
            
        }
        [MBProgressHUD showAlertViewWithText:responseObject[@"msg"]];
    } failure:^(NSError *error) {
        [MBProgressHUD showAlertViewWithText:@"获取失败，请稍后重试"];
        NSLog(@"错误信息 = %@",error);
    }];
}

- (void)ChangeTime{
    if (timerNum == 0) {
        timerNum = 60;
        self.TimeBtn.hidden = NO;
        self.TimeLabel.text = [NSString stringWithFormat:@"获取验证码"];
        [self.timer invalidate];
    }else{
        
        timerNum = timerNum - 1;
        self.TimeLabel.text = [NSString stringWithFormat:@"%lds后重新获取",timerNum];
    }
}

- (IBAction)ClickOnBtnToTiJiao:(id)sender {
    
    if (![MainObjectWay IsMobileWithString:self.UseriPhone.text]) {
        [MBProgressHUD showAlertViewWithText:@"请输入正确的手机号"];
        return;
    }
    
    if (self.CodeText.text.length == 0 ) {
        [MBProgressHUD showAlertViewWithText:@"请输入验证码"];
        return;
    }
    if (self.PayPW.text.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请输入密码"];
        return;
    }
    NSString * PW = [NSString stringWithFormat:@"%@",self.PayPW.text];
    NSString * PW01 = [NSString stringWithFormat:@"%@",self.PayPW01.text];
    
    if (![PW isEqual:PW01]) {
        [MBProgressHUD showAlertViewWithText:@"请保持两次密码一致"];
        return;
    }
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:self.UseriPhone.text forKey:@"phone"];
    [dic setObject:self.CodeText.text forKey:@"code"];
    [dic setObject:self.PayPW.text forKey:@"password"];
    
    [MainObjectWay PostWithMethod:@"pay_pwd" andAction:@"User" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"请求的信息 = %@",responseObject);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            [MBProgressHUD showAlertViewWithText:@"提交新密码信息"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [MBProgressHUD showAlertViewWithText:@"提交失败"];
            
        }
    } failure:^(NSError *error) {
        NSLog(@"错误信息 = %@",error);
    }];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
